home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15285 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  82 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Niklas Mellin <niklas.mellin@sto.fdata.se>
  4. Subject: Re: casting a void pointer back to a function pointer
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <3163BDF7.351C@sto.fdata.se>
  7. Date: Thu, 4 Apr 1996 12:17:59 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0 (WinNT; I)
  13. Organization: WM-data F÷rsvarsdata AB, Sweden
  14.  
  15. Daniel LaBell wrote:
  16. > I'm going to skip explaining why I want to do this, and get right to the
  17. > problem.  How do I cast a pointer to a function pointer?
  18. > Here is trivial example that shows the problem.
  19. > void foo1(){  cout << " foo1 " << endl; }
  20. > void foo2(){  cout << " foo2 " << endl; }
  21. > void foo3( int x )
  22. > { cout << " foo3, argument = " << x << endl; }
  23. > void do_a_foo( void (*fun)() )
  24. > {  (*fun)(); }
  25. > void do_a_foo2( int x,  void (*fun) (int ) )
  26. > {  (*fun) ( x ); }
  27. > int main()
  28. > {
  29. >   do_a_foo  ( foo1 );
  30. >   do_a_foo  ( foo2 );
  31. >   do_a_foo2 ( 2, foo3 );
  32. >   void * x=foo2;
  33. >   do_a_foo  (x); // I don't know the syntax to do this cast. :(
  34. >   return 0;
  35. > }
  36. > I get this warning message from g++:
  37. > :/home/student/dlabell/C/test.cc: In function `int main()':
  38. > :/home/student/dlabell/C/test.cc:21: warning: ANSI C++ forbids implicit conversion from `void *' in argument passing
  39. > :
  40. > :Compilation finished at Thu Apr  4 01:18:20
  41. > So, now my program isn't portable, and if I overload do_a_foo instead of
  42. > making do_a_foo2, it wouldn't compile at all.  So, I need to know the syntax
  43. > for casting a pointer to a pointer to a function.  Can this be done in a
  44. > portable way? ( how about for ANSI C? ) If not, can this be done in
  45. > g++/gcc? And also if it isnt ANSI wouldn't that violate the premise that
  46. > "any pointer can be cast to void * and back again without loss of
  47. > information"?  Or can I declare a type of pointer to a function?
  48. > --
  49. > Daniel LaBell
  50.  
  51. do_a_foo(x); // Daniel doesn't know the syntax to do this cast.
  52.  
  53. Examples of correct syntax:
  54.  
  55. do_a_foo(reinterpret_cast<void (*)()>(x)); // #1
  56. do_a_foo((void (*)())x);                   // #2
  57.  
  58. typedef void (*FPtr)();
  59. do_a_foo(reinterpret_cast<FPtr>(x));       // #3
  60. do_a_foo((FPtr)x);                         // #4
  61.  
  62. All 4 forms work on Borland 4.52 and I think all forms are supported by
  63. the draft standard and portable (at least on systems where function pointers 
  64. are of the same size as datapointers).
  65.  
  66. Pick the form you prefer, personally I would choose number 3, because
  67. I like the new casting syntax in C++, and I always use typedefs for
  68. function pointers. In C I would try number 4, and I expect it would
  69. work, but recently I've been doing a lot of C++, and very little
  70. plain C, so I don't know what ANSI C says about casting function pointers.
  71. For a better answer on the ANSI C question you should ask in comp.lang.c
  72.  
  73. ---
  74. Niklas Mellin
  75.